package com.zf.zffencheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zf.zffencheng.dao.AdminBankDao;
import com.zf.zffencheng.model.pojo.CapitalOrderProfit;
import com.zf.zffencheng.model.pojo.TAdminBank;
import com.zf.zffencheng.service.AdminBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * ClassName: AccountServiceImpl <br/>
 * Description:  <br/>
 * date: 2021/5/11 17:30<br/>
 *
 * @author william <br/>
 */
@Service
public class AdminBankServiceImpl extends ServiceImpl<AdminBankDao, TAdminBank> implements AdminBankService {
    @Autowired
    private AdminBankDao adminBankDao;
    @Override
    public TAdminBank get(Integer id,Integer adminId) {
        QueryWrapper<TAdminBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("adminId",adminId);
        return getOne(queryWrapper,false);
    }

    @Override
    public List<TAdminBank> listBank(Integer adminId,Integer page,Integer pageSize) {
        if(null == page || null == pageSize){
            page = 1;
            pageSize = 10;
        }
        IPage<TAdminBank> bankPage = new Page<>(page, pageSize);//参数一是当前页，参数二是每页个数
        QueryWrapper<TAdminBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adminId",adminId);
        queryWrapper.ne("status",0);
        queryWrapper.orderByDesc("Status","CreateTime");
        IPage<TAdminBank> bankPages = page(bankPage,queryWrapper);
        return bankPages.getRecords();
    }
    @Override
    public Integer listCount(Integer adminId) {
        QueryWrapper<TAdminBank> queryWrapper=new QueryWrapper();
        queryWrapper.eq("adminId",adminId);
        queryWrapper.ne("status",0);
        Integer count = adminBankDao.selectCount(queryWrapper);
        return null;
    }


    @Override
    public TAdminBank addBank(TAdminBank adminBank,Integer adminId) {
        if(null != adminBank){
            adminBank.setAdminId(adminId);
            adminBank.setCreateTime(new Date());
            TAdminBank adminBankSn = getStatusAdminBank(adminId);
            if(null != adminBankSn){
                if(adminBank.getStatus().equals((byte)2)){
                    adminBankSn.setStatus((byte)1);
                    updateById(adminBankSn);
                }
            }else{
                adminBank.setStatus((byte)2);
            }
            save(adminBank);
            return adminBank;
        }
        return null;
    }

    @Override
    public void deleteBank(Integer id,Integer adminId) {
        if(null != id){
            TAdminBank adminBank = get(id,adminId);
            if(null != adminBank && adminBank.getAdminId().equals(adminId)){
                adminBank.setStatus((byte)0);
                updateById(adminBank);
                //删除的记录时默认，把删除后的最新一条改成默认
                if(adminBank.getStatus().equals((2))) {
                    QueryWrapper<TAdminBank> queryWrapper = new QueryWrapper<>(); queryWrapper.select("sum(Fee) as fee ");
                    queryWrapper.eq("adminId", adminId);
                    queryWrapper.orderByDesc("createTime");
                    TAdminBank adminBankSu =  getOne(queryWrapper,false);
                    if(null != adminBankSu){
                        adminBankSu.setStatus((byte)2);
                        updateById(adminBankSu);
                    }
                }

            }

        }

    }

    @Override
    public TAdminBank updateBank(TAdminBank adminBank,Integer adminId) {
        if(null != adminBank.getId()){
            adminBank.setAdminId(adminId);
            updateById(adminBank);
            return adminBank;
        }
        return null;
    }

    @Override
    public TAdminBank updateDefault(Integer id,Integer adminId) {
        if(null != id){
            TAdminBank adminBank = get(id,adminId);
            TAdminBank adminBanks = getStatusAdminBank(adminId);
           if(null != adminBanks){
               if(null != adminBank
                       && !adminBank.getId().equals(adminBanks.getId())
                       && adminBank.getAdminId().equals(adminId)){
                   adminBank.setStatus((byte)2);
                   updateById(adminBank);
                   adminBanks.setStatus((byte)1);
                   updateById(adminBanks);
                   return adminBank;
               }
               return adminBanks;
           }else{
               if(null != adminBank && adminBank.getAdminId().equals(adminId)){
                   adminBank.setStatus((byte)2);
                   updateById(adminBank);
                   return adminBank;
               }
            }


        }
        return null;
    }
    public TAdminBank getStatusAdminBank(Integer adminId){
        QueryWrapper<TAdminBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adminId",adminId);
        queryWrapper.eq("status",(byte)2);
        return getOne(queryWrapper,false);

    }
}
